﻿using System.IO;

namespace Foundation
{
    /// <summary>
    /// 
    /// </summary>
    public static class FileNameProvider
    {
        /// <summary>
        /// Gets or sets the base directory.
        /// </summary>
        /// <value>
        /// The base directory.
        /// </value>
        public static string BaseDirectory { get; set; }

        /// <summary>
        /// Gets the file name for serialization.
        /// </summary>
        /// <typeparam name="TItem">The type of the item.</typeparam>
        /// <param name="extension">The extension.</param>
        /// <returns></returns>
        public static string GetFileNameForSerialization<TItem>(string extension = ".xml")
        {
            extension = extension.StartsWith(".") ? extension : string.Format(".{0}", extension);
            var fileName = string.Concat(typeof(TItem).Name, extension);
            var filePath = PathProvider.FindFileByName(fileName);
            return string.IsNullOrEmpty(BaseDirectory) ? filePath : Path.Combine(BaseDirectory, fileName);
        }

        /// <summary>
        /// Gets the file name for serialization.
        /// </summary>
        /// <typeparam name="TItem">The type of the item.</typeparam>
        /// <param name="item">The item.</param>
        /// <param name="extension">The extension.</param>
        /// <returns></returns>
        public static string GetFileNameForSerialization<TItem>(this TItem item, string extension = ".xml")
        {
            return GetFileNameForSerialization<TItem>(extension);
        }
    }
}
